﻿using B.S.BaseData.Api.Write.Applicantion.Command.GoodsCommand;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain.Entitys;
using MediatR;

namespace B.S.BaseData.Api.Write.Applicantion.CommandHandler.GoodsCommandHandler
{
    public class GoodsproductsImgAddCommandHandler : IRequestHandler<GoodsproductsImgAddCommand, APIResult<int>>
    {
        private readonly IBaseRepository<ImgInfo> _ImgRepository;
        public GoodsproductsImgAddCommandHandler(IBaseRepository<ImgInfo> imgRepository)
        {
            _ImgRepository = imgRepository;
        }
        public async Task<APIResult<int>> Handle(GoodsproductsImgAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            try
            {
                ImgInfo imgInfo=new ImgInfo();
                imgInfo.GoodsId= request.GoodsId;
                imgInfo.ImgUrl= request.ImgUrl;
                await _ImgRepository.CreateAsync(imgInfo);
                result.Code=ApiEnum.Success;
                result.Msg = "图片添加成功";
                result.Data = 1;
                return await Task.FromResult(result);
            }
            catch (Exception ex)
            {
                result.Code = ApiEnum.Fail;
                result.Msg = "图片添加失败";
                return await Task.FromResult(result);
            }
        }
    }
}
